__author__ = 'Janos G. Hajagos'

from DrawGraphObj import *
import random

class Form(object):
    """Abstract class for geometric shapes/forms in 3D"""
    object_graphic = None
    def obj_graphic(self):
        return self.object_graphic

class Cube(Form):
    def __init__(self,x,y,z,edge_length,material=None):
        """Create a geometric solid that has equal length sides"""
        self.object_graph = ObjGraphic()
        self.edge_length = edge_length
        el = self.edge_length

        #Front face   Back face   Left face   Right face  Bottom face   Top Face
        # 4 3         8  7        4  8        3 7         1 2           8 7
        # 1 2         5  6        1  5        2 6         5 6           4 3
        self.vertices = [[x,y,z],[x+el,y,z],[x+el,y,z+el],[x,y,z+el],[x,y+el,z],[x+el,y+el,z],[x+el,y+el,z+el],[x,y+el,z+el],]
        self.faces = [[1,2,3,4],[6,5,8,7],[5,1,4,8],[2,6,7,3],[5,6,2,1],[4,3,7,8]]

        self.object_graph.add_vertices(self.vertices)

        triangulated_faces = self.object_graph.convert_regular_quads_to_mesh(self.faces)

        texture_coordinates_list = [[1,2,3],[3,4,1],
            [1,2,3],[3,4,1],
            [1,2,3],[3,4,1],
            [1,2,3],[3,4,1],
            [1,2,3],[3,4,1],
            [1,2,3],[3,4,1],
            [1,2,3],[3,4,1]]

        self.object_graph.add_texture_coordinates(texture_coordinates_stretch())
        self.object_graph.add_face(triangulated_faces,normal_indices=triangulated_faces,texture_indices=texture_coordinates_list)

        normal_vector_list = self.object_graph.compute_normals_from_mesh()
        self.object_graph.add_normal_vectors(normal_vector_list)

        normal_indices = []
        for i in range(len(self.object_graph.normal_vectors)):
            normal_indices.append([i+1,i+1,i+1])

        self.object_graph.add_normal_indices(normal_indices)

        if material is not None:
            self.object_graph.associate_material(material)

    def center(self):
        el = self.edge_length
        return np.matrix([x + el / 2.0, y + el / 2.0, z + el / 2.0])


def texture_coordinates_stretch():
    return [[0,0],[1,0],[1,1],[0,1]]

class Quadrilateral(Form):
    """A basic triangulated Quadrilateral generating class"""
    def __init__(self,v1,v2,v3,v4, material=None):
        self.object_graph = ObjGraphic()
        self.vertices = [v1,v2,v3,v4]
        self.faces = [[1,2,3,4]]

        self.object_graph.add_vertices(self.vertices)
        self.object_graph.add_texture_coordinates(texture_coordinates_stretch())
        #self.object_graph.add_normal_vectors(normal_vector_from_triangular_face(v1,v2,v3))


        triangulated_coordinates = self.object_graph.convert_regular_quads_to_mesh(self.faces)

        self.object_graph.add_face(triangulated_coordinates,texture_indices = triangulated_coordinates)
        normal_vector_list = self.object_graph.compute_normals_from_mesh()
        self.object_graph.add_normal_vectors(normal_vector_list)

        normal_indices = []
        for i in range(len(self.object_graph.normal_vectors)):
            normal_indices.append([i+1,i+1,i+1])

        self.object_graph.add_normal_indices(normal_indices)
        self.object_graph.add_normal_indices(normal_indices)

        if material is not None:
            self.object_graph.associate_material(material)